package com.xjj.service.health.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjj.entity.*;
import com.xjj.service.health.service.HealthService;
import com.xjj.util.utils.JwtUtil;
import com.xjj.vo.HealthVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther: AYang
 * @Date: 2022/10/28 09:28
 * @Description:
 */
@Api(tags = "健康管理相关接口")
@RestController
@RequestMapping("/health/admin")
@Slf4j
public class AdminHealthController {
    @Autowired
    private HealthService healthService;

    //带药提醒列表
    @PostMapping("/list")
    public ResponseEntity<PageBean<Health>> list(@RequestBody HealthVo healthVo, HttpServletRequest request) throws JsonProcessingException {
        Manager manger = getManager(request.getHeader("token"));
        Integer id = manger.getId();
        log.debug("当前账号老师id："+id);
        log.debug("healthVo:{}" + healthVo);
        Integer pageSize = 6;
        Health health = healthVo.getHealth();
        Integer pageNum = healthVo.getPageNum() == null ? 1 : healthVo.getPageNum();
        PageBean<Health> pageBean = healthService.getByPage(health, pageNum, pageSize);
        return ResponseEntity.SUCCESS("获取列表成功", pageBean);
    }

    // 修改阅读状态
    @PostMapping("/update/{id}")
    public ResponseEntity update(@PathVariable Integer id){
        Health health = new Health();
        health.setId(id);
        health.setStatus("y");
        health.setCheckTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        // 根据id修改状态
        healthService.updateStatusById(health.getId(),health.getStatus(),health.getCheckTime());
        return ResponseEntity.SUCCESS("ok",null);
    }

    private Manager getManager(String token) throws JsonProcessingException {
        System.out.println(token);
        //解析出来Manager
        Manager manager = JwtUtil.getManager(token);
        System.out.println("manager" + manager);
        return manager;
    }
}
